#ifndef __MUTEX_H__
#define __MUTEX_H__

#include <pthread.h>
#include <cassert>

class Mutex
{
public:
    Mutex()
    {
        int err = pthread_mutex_init(&mMutex, NULL);
        assert(err == 0); (err);
    }
    ~Mutex()
    {
        pthread_mutex_destroy(&mMutex);
    }
    void Lock()
    {
        pthread_mutex_lock(&mMutex);
    }
    void UnLock()
    {
        pthread_mutex_unlock(&mMutex);
    }
    pthread_mutex_t *GetPthreadMutex()
    {
        return &mMutex;
    }

private:
    pthread_mutex_t mMutex;

private:
    Mutex(const Mutex&);
    Mutex& operator=(const Mutex&);
};

class MutexGruad
{
public:
    MutexGruad(Mutex &mutex)
        : mMutex(mutex)
    {
        mMutex.Lock();
    }
    ~MutexGruad()
    {
        mMutex.UnLock();
    }

private:
    Mutex &mMutex;

private:
    MutexGruad(const MutexGruad&);
    MutexGruad& operator=(const MutexGruad&);
};

#endif
